/**
 *
 */
package com.wedroid.r2d2.support;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * @author lute
 *
 */
public class BaseRequest {

    private final String sid;

    private final String uin;

    private final String skey;

    private final String deviceId;

    private Map<String, Object> data;

    /**
     * @param sid
     * @param uin
     * @param skey
     */
    public BaseRequest(String sid, String uin, String skey) {
        this.sid = sid;
        this.uin = uin;
        this.skey = skey;
        this.deviceId = "e" + RandomStringUtils.randomNumeric(15);

        data = new HashMap<String, Object>();
        data.put("Uin", this.uin);
        data.put("Sid", this.sid);
        data.put("Skey", this.skey);
        data.put("DeviceID", this.deviceId);
    }

    /**
     * @return
     */
    public boolean isValid() {
        return StringUtils.isNotBlank(sid) &&
                StringUtils.isNotBlank(uin) &&
                StringUtils.isNotBlank(skey) &&
                StringUtils.isNotBlank(deviceId);
    }

    /**
     * @return
     */
    public Map<String, Object> build() {
        Map<String, Object> baseRequest = new HashMap<String, Object>();
        baseRequest.put("BaseRequest", data);
        return baseRequest;
    }

    /**
     * Change uin parameter from string to integer
     * @return
     */
    public BaseRequest changeUinToInteger() {
        data.put("Uin", Long.valueOf(uin));
        return this;
    }

    /**
     * @return
     */
    public Map<String, Object> get() {
        return Collections.unmodifiableMap(data);
    }

    /**
     * @return the sid
     */
    public String getSid() {
        return sid;
    }

    /**
     * @return the uin
     */
    public String getUin() {
        return uin;
    }

    /**
     * @return the skey
     */
    public String getSkey() {
        return skey;
    }

    /**
     * @return the deviceId
     */
    public String getDeviceId() {
        return deviceId;
    }

}
